var ajax = require('../../utils/ajax.js');
var wxUtil = require('../../utils/wxUtil.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');
var app = getApp();


Page(common.extendPage(MPage, {
	data: {
		winHei: 0,
		dataLoaded: false,
	},

	onLoad(options) {
		this.techId = options.techId; //技师id
		this.orderId = options.orderId; //订单 id

		if (this.techId) {
			this._getWaiter();
		} else if (this.orderId) {
			this._getOrder();
		}
	},

	onShow() {
		var preTimeObj = app.globalData.preTimeObj;

		if (preTimeObj) {
			this.setData({
				preTimeObj
			});
			app.globalData.preTimeObj = null;
		}
	},

	_jump(evt) {
		common.jump(evt);
	},

	/**
	 * 获取技师信息
	 */
	_getWaiter() {
		var _self = this;

		ajax.P('/openapi/CTechnician/bespeakPage', {
			technician_id: _self.techId
		}, function(res) {
			var data = res.result;
			var {
				storeInfo,
				techInfo,
				timeList
			} = data;

			_self.setData({
				dataLoaded: true,
				winHei: app.getWinHei(),
				storeInfo: {
					addr: storeInfo.province_name + storeInfo.city_name + storeInfo.district_name + storeInfo.detail_addr
				},
				techInfo: {
					name: techInfo.name,
					avatar_img: techInfo.avatar_img,
					head_img: techInfo.head_img,
					pre_price: techInfo.pre_price,
					title: techInfo.title,
					phone: techInfo.phone,
				},
				techId: _self.techId,
			});
		});
	},

	//获取订单信息
	_getOrder() {
		var _self = this;

		ajax.P('/openapi/CTechnician/bespeakInfo', {
			id: _self.orderId
		}, function(res) {
			var data = res.result;
			var appointInfo = data.bespeakInfo;

			_self.setData({
				dataLoaded: true,
				winHei: app.getWinHei(),
				storeInfo: {
					addr: data.storeArr
				},
				techInfo: {
					name: data.techName,
					head_img: data.headImg,
					pre_price: appointInfo.pre_price,
					title: data.techTitle,
					phone: data.techPhone,
				},
				order: {
					orderNo: data.bespeakId,
					createTime: appointInfo.createTime,
					stateName: appointInfo.stateName,
					payState: appointInfo.pay_state,
					appointTime: appointInfo.date + ' ' + appointInfo.beginTime + '-' + appointInfo.endTime,
					remark: appointInfo.remark,
				},

				orderId: _self.orderId,
			});
		});
	},

	//预约
	_appoint(evt) {
		var _self = this;
		var formData = evt.detail.value || {};
		var formId = evt.detail.formId;

		formData['technician_id'] = _self.techId;

		if (_self.orderId) {
			_self._payAgain();
		} else {
			_self._pay(formData);
		}
		common.submitForm(formId);
	},

	_pay(formData) {
		var _self = this;

		ajax.P('/openapi/CTechnician/doBespeak', formData, function(res) {
			var data = res.result;

			_self.orderId = data.bespeakId;
			_self._wxPay(JSON.parse(data.wxPayParams));
		});
	},

	_payAgain() {
		var _self = this;

		ajax.P('/openapi/CTechnician/payAgain', {
			id: _self.orderId
		}, function(res) {
			var data = res.result;
			_self._wxPay(JSON.parse(data.wxPayParams));
		});
	},

	/**
	 * 支付成功回调
	 */
	_wxPay(wxPayParams) {
		wxPayParams.success = function() {
			wx.redirectTo({
				url: '/pages/appointOrderList/appointOrderList?ty=0'
			});
		}
		wxUtil.wxPay(wxPayParams);
	},

	_callUser(evt) {
		var {
			userPhone
		} = evt.currentTarget.dataset

		wx.makePhoneCall({
			phoneNumber: userPhone
		});
		common.submitForm(evt.detail.formId);
	},
}));